//
//  MKUICollectionViewModel+MKUICollectionViewWaterFlowLayout.m
//  MKUI
//
//  Created by 苏尚进 on 2023/2/28.
//  Copyright © 2023 moon. All rights reserved.
//

#import "MKUICollectionViewModel+MKUICollectionViewWaterFlowLayout.h"
#import "MKUICollectionViewWaterFlowLayoutTitleSupplementarySectionModel.h"
@implementation MKUICollectionViewModel (MKUICollectionViewWaterFlowLayout)
- (CGSize)collectionView:(UICollectionView *)collectionView waterFlowLayout:(MKUICollectionViewWaterFlowLayout *)collectionViewLayout itemSizeForItemAtIndexPath:(NSIndexPath *)indexPath fits:(CGSize)fitsSize{
    id<MKUICollectionViewDelegateWaterFlowLayout> forwardDelegate = nil;
    if([self.forwardDelegate conformsToProtocol:@protocol(MKUICollectionViewDelegateWaterFlowLayout)]){
        forwardDelegate = (id<MKUICollectionViewDelegateWaterFlowLayout>)self.forwardDelegate;
    }
    CGRect bounds = collectionView.bounds;
    CGSize size = CGSizeZero;
    if([collectionViewLayout isKindOfClass:[MKUICollectionViewWaterFlowLayout class]]){
        MKUICollectionViewWaterFlowLayout *flowlayout = (MKUICollectionViewWaterFlowLayout *)collectionViewLayout;
        if(CGSizeEqualToSize(bounds.size,CGSizeZero)){//flowlayout布局时,0尺寸会出现"the item height must be less that the height of the UICollectionView minus the section insets top and bottom values."的警告,因此直接设置0的itemsize
            size = CGSizeZero;
        }else{
            size = flowlayout.itemSize;
        }
    }
    if([forwardDelegate respondsToSelector:_cmd]){
        size = [forwardDelegate collectionView:collectionView waterFlowLayout:collectionViewLayout itemSizeForItemAtIndexPath:indexPath fits:fitsSize];
    }else{
        MKUICollectionViewCellModel *cm = [self cellModelAtIndexPath:indexPath];
        Class cellClass = cm.cellClass;
        if([cellClass respondsToSelector:@selector(sizeWithCollectionView:collectionCellModel:fits:)]){
            size = [cellClass sizeWithCollectionView:collectionView collectionCellModel:cm fits:fitsSize];
        }
    }
    return size;
}

- (CGSize)collectionView:(UICollectionView *)collectionView waterFlowLayout:(MKUICollectionViewWaterFlowLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    id<MKUICollectionViewDelegateWaterFlowLayout> forwardDelegate = nil;
    if([self.forwardDelegate conformsToProtocol:@protocol(MKUICollectionViewDelegateWaterFlowLayout)]){
        forwardDelegate = (id<MKUICollectionViewDelegateWaterFlowLayout>)self.forwardDelegate;
    }
    NSString *kind = UICollectionElementKindSectionHeader;
    MKUICollectionViewWaterFlowLayout *flowLayout = (MKUICollectionViewWaterFlowLayout *)collectionViewLayout;
    CGSize size = flowLayout.headerReferenceSize;
    
    if([forwardDelegate respondsToSelector:_cmd]){
        size = [forwardDelegate collectionView:collectionView waterFlowLayout:collectionViewLayout referenceSizeForHeaderInSection:section];
    }else{
        MKUICollectionViewSectionModel *sm = (MKUICollectionViewSectionModel *)[self sectionModelAtIndex:section];
        Class aClass = [sm supplementaryElementViewClassForKind:kind];
        if(aClass&&[aClass conformsToProtocol:@protocol(MKUICollectionViewSupplementaryElementProtocol)]){
            if([aClass respondsToSelector:@selector(referenceSizeWithCollectionView:collectionSectionModel:forKind:)]){
                size = [aClass referenceSizeWithCollectionView:collectionView collectionSectionModel:sm forKind:kind];
            }
        }
    }
    return size;
}

- (CGSize)collectionView:(UICollectionView *)collectionView waterFlowLayout:(MKUICollectionViewWaterFlowLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    id<MKUICollectionViewDelegateWaterFlowLayout> forwardDelegate = nil;
    if([self.forwardDelegate conformsToProtocol:@protocol(MKUICollectionViewDelegateWaterFlowLayout)]){
        forwardDelegate = (id<MKUICollectionViewDelegateWaterFlowLayout>)self.forwardDelegate;
    }
    NSString *kind = UICollectionElementKindSectionFooter;
    MKUICollectionViewWaterFlowLayout *flowLayout = (MKUICollectionViewWaterFlowLayout *)collectionViewLayout;
    CGSize size = flowLayout.footerReferenceSize;
    
    if([forwardDelegate respondsToSelector:_cmd]){
        size = [forwardDelegate collectionView:collectionView waterFlowLayout:collectionViewLayout referenceSizeForFooterInSection:section];
    }else{
        MKUICollectionViewSectionModel *sm = (MKUICollectionViewSectionModel *)[self sectionModelAtIndex:section];
        Class aClass = [sm supplementaryElementViewClassForKind:kind];
        if(aClass&&[aClass conformsToProtocol:@protocol(MKUICollectionViewSupplementaryElementProtocol)]){
            if([aClass respondsToSelector:@selector(referenceSizeWithCollectionView:collectionSectionModel:forKind:)]){
                size = [aClass referenceSizeWithCollectionView:collectionView collectionSectionModel:sm forKind:kind];
            }
        }
    }
    return size;
}
- (nullable MKUICollectionViewWaterFlowLayoutLastLineItemOption *)collectionView:(UICollectionView *)collectionView waterFlowLayout:(MKUICollectionViewWaterFlowLayout *)collectionViewLayout referenceOptionForLastLineItemInSection:(NSInteger)section{
    id<MKUICollectionViewDelegateWaterFlowLayout> forwardDelegate = nil;
    if([self.forwardDelegate conformsToProtocol:@protocol(MKUICollectionViewDelegateWaterFlowLayout)]){
        forwardDelegate = (id<MKUICollectionViewDelegateWaterFlowLayout>)self.forwardDelegate;
    }
    NSString *kind = MKUICollectionElementKindWaterFlowSectionLastLineItem;
    MKUICollectionViewWaterFlowLayout *flowLayout = (MKUICollectionViewWaterFlowLayout *)collectionViewLayout;
    MKUICollectionViewWaterFlowLayoutLastLineItemOption *option = flowLayout.lastLineItemReferenceOption;
    
    if([forwardDelegate respondsToSelector:_cmd]){
        option = [forwardDelegate collectionView:collectionView waterFlowLayout:collectionViewLayout referenceOptionForLastLineItemInSection:section];
    }else{
        MKUICollectionViewSectionModel *sm = (MKUICollectionViewSectionModel *)[self sectionModelAtIndex:section];
        Class aClass = [sm supplementaryElementViewClassForKind:kind];
        if(aClass&&[aClass conformsToProtocol:@protocol(MKUICollectionViewWaterFlowLayoutLastLineItemSupplementaryProtocol)]){
            if([aClass respondsToSelector:@selector(referenceOptionForLastLineItemWithCollectionView:collectionSectionModel:forKind:)]){
                option = [aClass referenceOptionForLastLineItemWithCollectionView:collectionView collectionSectionModel:sm forKind:kind];
            }
        }
    }
    return option;
}
@end
